<template>
  <el-dialog
    :visible.sync="visible"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'"
    >
      <el-row :gutter="30">
          <el-col :span="12">
          <el-form-item label="房型名称" prop="name">
            <el-input v-model="dataForm.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="简称" prop="shortName">
            <el-input v-model="dataForm.shortName"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="可住人数" prop="checkinGuests">
            <el-input-number
              v-model="dataForm.checkinGuests"
              :min="0"
              label="可住人数"
            ></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="可携带儿童数" prop="childCheckinNums">
            <el-input-number
              v-model="dataForm.childCheckinNums"
              :min="0"
              label="可携带儿童数"
            ></el-input-number>
          </el-form-item>
        </el-col>
       
      </el-row>
      <el-row :gutter="30">
         <!-- <el-col :span="12">
          <el-form-item label="房间类型" prop="rentType">
            <el-radio-group v-model="dataForm.rentType">
              <el-radio :label="1">整栋</el-radio>
              <el-radio :label="2">单间</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item label="房间数量(间)" prop="bedroom">
            <el-input
              v-model="dataForm.bedroom"
              type="number"
              min="0"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客厅(个))" prop="livingroom">
            <el-input
              v-model="dataForm.livingroom"
              type="number"
              min="0"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="卫生间(个)" prop="bathroom">
            <el-input
              v-model="dataForm.bathroom"
              type="number"
              min="0"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="床型" prop="">
            <div
              class="item"
              v-for="(item, index) in dataForm.bedData"
              :key="index"
              style="margin-bottom: 10px"
            >
              <el-input
                v-model="item.category"
                placeholder="请输入床型(如单人床)"
                style="width: 200px"
              >
              </el-input>
              <el-input
                v-model="item.size"
                placeholder="请输入床尺寸(如1.5)"
                style="width: 200px; margin: 0 15px"
              >
              </el-input>
              <el-input-number
                v-model="item.num"
                :min="1"
                label=""
              ></el-input-number>
              <el-link
                type="danger"
                style="margin-left: 10px"
                @click="delBed(index)"
                >删除</el-link
              >
            </div>
            <el-button type="primary" @click="addBed()">新增床型</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="面积: ㎡" prop="area">
            <el-input v-model="dataForm.area" type="text" min="0"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="房源景观" prop="sceneCode">
            <ren-select
              v-model="dataForm.sceneCode"
              dict-type="HouseScene"
              placeholder="请选择"
            ></ren-select>
          </el-form-item>
        </el-col>
      
        <el-col :span="24">
          <el-form-item label="房间号设置" prop="roomFloor" class="rooms">
            <div
              class="item"
              v-for="(item, index) in dataForm.roomFloor"
              :key="index"
            >
              楼层：<el-input
                placeholder="楼层如1"
                v-model="dataForm.roomFloor[index].floor"
                style="width: 100px; margin-right: 15px"
              ></el-input>
              房间号：<el-input
                placeholder="房号如101"
                v-model="dataForm.roomFloor[index].roomNumber"
                style="width: 100px; margin-right: 15px"
              ></el-input>
              <i class="el-icon-error" @click="del(index)"></i>
            </div>

            <el-button type="primary" @click="addRoomNo()">新增</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false"> {{ $t("cancel") }}</el-button>

      <el-button type="primary" @click="dataFormSubmitHandle()"
        >{{ $t("confirm") }}
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
import template from "../devtools/template.vue";
export default {
  components: {
    template,
  },
  data() {
    return {
      visible: false,
      dataForm: {
         bedData: [
          {
            category: "",
            size: "",
            num: "",
          },
        ],
        rentType:2,
        bedroom:'',
        livingroom:'',
        bathroom:'',
        area:'',
        sceneCode:'',
        roomFloor: [],
        id: "",
        homestayId: this.$store.state.user.homestayId,
        name: "",
        shortName: "",
        checkinGuests: "",
        description: "",
        childCheckinNums: "",
      },
    };
  },
  watch: {
    // "dataForm.ro"(newval, oldval) {
    //     var hash = {};
    //     for (var i in newval) {
    //         if (hash[newval[i]]) {
    //             return this.$message.error('请勿添加重复房号' + newval[i]);;
    //         } else {
    //             hash[newval[i]] = true;
    //         }
    //     }
    // }
  },
  computed: {
    dataRule() {
      return {
        name: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
         rentType:[
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        bedroom:[
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        livingroom:[
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        bathroom:[
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        area:[
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        sceneCode:[
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        roomFloor:[
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
      };
    },
  },
  methods: {
    addRoomNo() {
      this.dataForm.roomFloor.push({
        floor: "",
        roomNumber: "",
      });
    },
    del(ind) {
      this.dataForm.roomFloor.splice(ind, 1);
    },
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        this.dataForm.bedData = [];
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
     addBed() {
      this.dataForm.bedData.push({
        category: "",
        size: "",
        num: "",
      });
    },
    delBed(i) {
      this.dataForm.bedData.splice(i, 1);
    },
    // 获取信息
    getInfo() {
      this.$http
        .get("/hotel/house/base/" + this.dataForm.id)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          if (this.dataForm.roomFloor.length == 0) {
            return this.$message.error("请设置房间号");
          }
          //判断房间号是否都填了
          let d=this.dataForm.roomFloor.map((item)=>{
            return !item.floor||!item.roomNumber
          })
          if(d.indexOf(true)>-1){
             return this.$message.error("请完整设置房间号");
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/hotel/house/base",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      {
        leading: true,
        trailing: false,
      }
    ),
  },
};
</script>

<style lang="scss" scoped>
h3 {
  padding: 30px 0 15px;
  width: 94%;
  margin: 0 auto 30px;
  border-bottom: 1px solid #ddd;
  color: #303133;
  font-size: 22px;
  overflow: hidden;
}

.rooms .item {
  display: inline-block;
  margin-right: 15px;
  margin-bottom: 10px;
  position: relative;
  background: #f5f5f5;
  padding: 5px 10px;

  .el-icon-error {
    font-size: 20px;
    color: #dd0606;
  }
}
</style>
